package com.csw.tourismInformation.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.csw.tourismInformation.dao.ProvinceDao;
import com.csw.tourismInformation.entity.Province;

/**
 *
 * @author Administrator
 * @date 2019/10/23
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProvinceServiceImpl implements ProvinceService {
	@Autowired
	ProvinceDao provinceDao;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Integer countPage(Integer pageSize) {
		Integer countPage = 0;
		Integer count = provinceDao.queryCount();
		if (count % pageSize == 0) {
			countPage = count / pageSize;
		} else {
			countPage = count / pageSize + 1;
		}
		return countPage;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<Province> queryAllAndByPage(Integer currPage, Integer pageSize, Integer countPage) {
		Integer firstIndex;
		Integer lastIndex;
		if (!currPage.equals(countPage)) {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = currPage * pageSize;
		} else {
			firstIndex = (currPage - 1) * pageSize + 1;
			lastIndex = provinceDao.queryCount();
		}
		return provinceDao.queryAllAndByPage(firstIndex, lastIndex);
	}

	@Override
	public void deleteById(Integer pid) {
		provinceDao.deleteById(pid);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public Province queryById(Integer pid) {
		return provinceDao.queryById(pid);
	}

	@Override
	public void updateById(Province province) {
		provinceDao.updateById(province);
	}

	@Override
	public void addProvince(Province province) {
		provinceDao.addProvince(province);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<Province> queryAll() {
		return provinceDao.queryAll();
	}

	@Override
	public void updateSpots(Province province) {
		provinceDao.updateSpots(province);
	}

}
